import {TYPES,ADD_BACK,UPDATE,RM_TYPE} from '../actions/types.action';

const initState = {
	types:[]
}

function Types(state=initState,action) {
	switch (action.type){
		case TYPES:
			return {...state,...{types:action.types}};
		case UPDATE:
			state.types.forEach(tp=>{
				if (tp.id===action.data.id) {
					tp.name=action.data.name;
				}
			});
			return {...state,...{types:[...state.types]}};
		case ADD_BACK:
			action.res.newData=1;
			return {...state,...{types:[...state.types,...[action.res]]}};
		case RM_TYPE:
			let types=state.types.filter(tp=>tp.id!==action.id);
			return {...state,...{types}}
		default :
			return {...state};
	}
}

export default Types;